<template>
  <div>
    <van-nav-bar
      title="账号注册"
      left-arrow
      @click-left="onClickLeft"
    />
    <van-form @submit="onSubmit">
      <van-field
        v-model="form.username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{
          required: true,
          message: '请输入用户名',
          pattern: /^\w/
        }]"
      />
      <van-field
        v-model="form.password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{
          required: true,
          message: '请填写密码',
          pattern: /^\w/
        }]"
      />
      <!-- 正则校验未完成 -->
      <van-field
        v-model="form.passwordAgain"
        type="password"
        name="密码"
        label="密码"
        placeholder="再次输入密码"
        :rules="[{
          required: true,
          message: '两次输入的密码不一致',
          pattern: /^\w/
        }]"
      />
      <div style="margin: 16px;">
        <van-button block type="info" native-type="submit">注册</van-button>
      </div>
    </van-form>
    <div class="go" @click="onSignin">去登录</div>
  </div>
</template>

<script>
import { userRegister } from '@/api/user.js'
import { Toast } from 'vant'
export default {
  data () {
    return {
      form: {
        username: '',
        password: '',
        passwordAgain: ''
      }
    }
  },

  created () {

  },

  methods: {
    async onSubmit () {
      try {
        const res = await userRegister(this.form)
        console.log(res)
        if (res.request.status === 200) {
          Toast.success(res.data.description)
          return true
        }
      } catch (err) {
        Toast.fail('服务器繁忙')
      }
    },
    onClickLeft () {
      this.$router.push({
        path: '/my'
      })
    },
    onSignin () {
      this.$router.push({
        path: '/login'
      })
    }
  }
}
</script>

<style scoped lang='less'>
/deep/.van-nav-bar__content {
  background-color: #16A77A;
  color: #fff;
}
/deep/.van-nav-bar__title {
  color: #fff;
}
/deep/.van-button--info {
  background-color: #0FA376;
  border: 1px solid #0FA376;
}
/deep/.van-icon-arrow-left:before {
  color: #fff;
}
.go {
  text-align: center;
  color: #9F9699;
  font-size: 32px;
}
</style>
